//
//  DoggyListViewController.swift
//  MVP_Demo
//
//  Created by mac on 2017/7/28.
//  Copyright © 2017年 LLyan. All rights reserved.
//

import UIKit

class DoggyListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var emptyView: UIView?
    @IBOutlet weak var tableView: UITableView?
    @IBOutlet weak var spinner: UIActivityIndicatorView?
    
    fileprivate let dogPresenter = DoggyPresenter(dogService: DoggyService())
    fileprivate var dogsToDisplay = [DoggyViewData]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        tableView?.dataSource = self
        tableView?.delegate = self
        tableView?.tableFooterView = UIView(frame: CGRect.zero)
        spinner?.hidesWhenStopped = true
        dogPresenter.attachView(true, view: self)
        dogPresenter.getDogs()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: - tableViewDatasource
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        print(dogsToDisplay)
        return dogsToDisplay.count;
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        let userViewData = dogsToDisplay[indexPath.row]
        cell.textLabel?.text = userViewData.name
        cell.textLabel?.textColor = UIColor.red
        cell.detailTextLabel?.text = userViewData.age
        return cell
    }

    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension DoggyListViewController: DoggyView{
    
    func startLoading() {
        spinner?.startAnimating()
    }
    
    func finishLoading() {
        spinner?.stopAnimating()
    }
    
    func setDoogies(_ doggies: [DoggyViewData]) {
        dogsToDisplay = doggies
        tableView?.isHidden = false
//        tableView?.isHidden = true
        tableView?.reloadData()
    }
    
    func setEmpty() {
//        tableView?.isHidden = true
        tableView?.isHidden = false
    }
}



